package com.ruoyi.ftrl.io.server;



import com.ruoyi.ftrl.io.server.common.ByteUtil;
import com.ruoyi.ftrl.io.server.domain.MyPacket;
import com.ruoyi.ftrl.io.server.domain.SendPacket;
import org.tio.core.ChannelContext;
import org.tio.core.exception.TioDecodeException;
import org.tio.core.intf.Packet;
import org.tio.server.intf.TioServerHandler;

import java.nio.ByteBuffer;

/**
 * 消息解码、编码
 *
 * @author asurplus
 */
public abstract class AbstractAioHandler implements TioServerHandler {

    /**
     * 解码 首先把接受到的bytebuffer解码成可以识别的业务包
     * 消息结构 消息头+消息体
     * 消息头:无
     * limit 消息的最后位置
     * position 消息的起始位置
     * 消息体:json串的byte[]
     *
     * @param
     * @param channelContext
     * @return
     * @throws
     */
    @Override
    public Packet decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext) throws TioDecodeException {
        //System.out.println("buffer = " + Arrays.toString(buffer.array()));
//        String s1 = ByteUtil.bytesToHex(buffer.array());
//          System.err.println("s1 = " + s1);
        //收到的数据组不成业务包,则返回null告诉框架数据不够
        if (readableLength < MyPacket.HEADER_LENGTH) {
            return null;
        }
        //获取消息体长
        int bodyLength = buffer.get(6);
        //消息体长度不正确
        if (bodyLength <= 0) {
            buffer.clear();
            throw new TioDecodeException("bodyLength与" + channelContext.getClientNode() + "不准确");
        }

        //System.out.println("buffer.array().toString() = " + Arrays.toString(buffer.array()) );
        //  System.out.println("buffer = " + buffer); //
        //System.out.println("buffer.array().toString() = " + Arrays.toString(buffer.array()) );
        //收到所有数据 byteBUffer  开始解码  解码需要 提取所有数据转换成 16进制数据  然后截取字符串 塞入packet  在控制台输出  打印出来

        //计算本次需要的数据长度  总长度
        int neededLength = MyPacket.HEADER_LENGTH + bodyLength + 1;

        //收到的数据是否足够组包
        int isDataEnough = readableLength - neededLength;
        if (isDataEnough < 0) {
            return null;
        } else {
            MyPacket imPacket = new MyPacket();
            if (bodyLength > 0) {
                byte[] dst = new byte[bodyLength + MyPacket.HEADER_LENGTH + 1];
                buffer.get(dst);
                String s = ByteUtil.bytesToHex(dst);
                imPacket.setText(s);
                imPacket.setBody(dst);
            }
            return imPacket;
        }
    }

    /**
     * 编码：把业务消息包编码为可以发送的ByteBuffer
     * 总的消息结构：消息头 + 消息体
     * 消息头结构：    6个字节，存储消息体的长度
     * 消息体结构：   对象的json串的byte[]
     */
    @Override
    public ByteBuffer encode(Packet packet, org.tio.core.TioConfig tioConfig, ChannelContext channelContext) {
        //    MyPacket myPacket=new MyPacket();
        //  byte[] body=myPacket.getBody();
        SendPacket sendPacket = (SendPacket) packet;
        String body = sendPacket.getBody();


        byte[] bytes = ByteUtil.hexStringToByteArray(body);

        //创建一个新的byteBuffer
        ByteBuffer buffer = ByteBuffer.allocate(bytes.length);
        // buffer.order(tioConfig.getByteOrder());

        //
        ////char buff_send[BUFSIZ * 4] = { 0xEB,0x90,0xEB,0x90,0xEB,0x90,0x06,0x18,0xA3,0x00,0x01,0x99,0x55,'\0' };//关阀门01
        ////char buff_send[BUFSIZ * 4] = { 0xEB,0x90,0xEB,0x90,0xEB,0x90,0x06,0x18,0xA3,0x00,0x02,0x99,0x56,'\0' };//关阀门02
        ////char buff_send[BUFSIZ * 4] = { 0xEB,0x90,0xEB,0x90,0xEB,0x90,0x06,0x18,0xA3,0x00,0x01,0x55,0x11,'\0' };//开阀门01
        //char buff_send[BUFSIZ * 4] = { 0xEB,0x90,0xEB,0x90,0xEB,0x90,0x06,0x18,0xA3,0x00,0x02,0x55,0x12,'\0' };//开阀门02
        ////char buff_send[BUFSIZ * 4] = { 0xEB,0x90,0xEB,0x90,0xEB,0x90,0x05,0x01,0xA3,0x00,0x01,0xA5,'\0'};//读数据
        //
        //

        if (bytes.length > 0) {
            buffer.put(bytes);
        }
//        System.out.println("buffer.toString() = " + buffer.toString());
//        System.out.println("buffer.array().toString() = " + Arrays.toString(buffer.array()));
        return buffer;
    }

}
